package com.yrmao.chiefenergyofficer.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by yemao on 16/11/16.
 */

public class HanderUtil {
    private static Context mContext;
    private static HanderUtil mHanderUtil;
    private int handler_state = 0;
    private int HANDLER_STATE_0 = 0;
    private int HANDLER_STATE_1 = 1;
    private int HANDLER_STATE_2 = 2;
    private int HANDLER_STATE_3 = 3;
    private Boolean isPause = false;
    private Thread thread;
    private int runTime = 3 * 1000;
    private ViewPager mViewPager;
    private int possition = 0;

    public HanderUtil(Context context) {
        mContext = context;
        if (thread == null) {
            thread = new Thread(mRunnalbe);
            thread.start();
        }
    }

    public static HanderUtil getInstence(Context context) {
        if (mHanderUtil == null) {
            mHanderUtil = new HanderUtil(context);
        }
        if (mContext == null) {
            mContext = context;
        }
        return mHanderUtil;
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            int state = msg.arg1;
            possition = msg.arg2;
            switch (state) {
                case 0://开始
                    mViewPager.setCurrentItem(possition);
                    break;
                case 1://暂停
                    possition--;
                    break;
                case 2://停止
                    handler.removeCallbacks(mRunnalbe);
                    break;
                case 3:
                    break;

            }
        }
    };
    Runnable mRunnalbe = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(mRunnalbe, runTime);
            Message msg = handler.obtainMessage();
            possition++;
            msg.arg1 = handler_state;
            msg.arg2 = possition;
            handler.handleMessage(msg);
        }
    };

    public void startHander() {
        isPause = false;
        handler_state = HANDLER_STATE_0;
    }

    public void pauseHander() {
        isPause = true;
        handler_state = HANDLER_STATE_1;
    }

    public void stopHander() {
        handler_state = HANDLER_STATE_2;
    }

    public void setRunnableTime(int runnableTime) {
        this.runTime = runnableTime;
    }

    public void setViewPager(ViewPager viewPager) {
        mViewPager = viewPager;
        setCurrentItem(mViewPager.getCurrentItem());
        mViewPager.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        pauseHander();
                        break;
                    case MotionEvent.ACTION_MOVE:
                        break;
                    case MotionEvent.ACTION_UP:
                        startHander();
                        break;
                }
                return false;
            }
        });
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setCurrentItem(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    public void setCurrentItem(int possition) {
        this.possition = possition;
    }

}
